    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Site extends CI_Controller {

	public function index()
	{
        $this->load->model('adModel');
        $data['records'] = $this->adModel->get_categories();
		$this->load->view('header');

        if($this->session->userdata('logged_in')) {
            $this->load->view('nav_user');
        }
        else
		$this->load->view('nav');
		$this->load->view('home_content',$data);
        $this->load->view('sign_in');
        $this->load->view('footer');
	}

    public function category($sort_by, $order_by, $cat)
    {
        if($sort_by=='0')
        $sort_by = 'ad_id';

        if($order_by=='0')
        $order_by = 'ASC';

        $this->load->library('pagination'); 
        $this->load->model('adModel');

        //configuring pagination
        $config = $this->style_pagination();
        $config['base_url'] = base_url()."Site/category/$sort_by/$order_by/$cat";
        $config['per_page'] = 12;
        $config['num_links'] = 5;
        $config['uri_segment'] = 6;


        //getting data from db
        $data = $this->adModel->get_records($config['per_page'], $this->uri->segment(6), $sort_by, $order_by, $cat);

        $config['total_rows'] = $data['num_rows'];

        //init pagination
        $this->pagination->initialize($config);

        //cat to view
        $data['cat'] = $cat;
        $data['Heading'] = $this->adModel->get_category_name($cat);

        //loading views        
        $this->load->view('header');
        if($this->is_logged_in()){
            $this->load->view('static_nav');
        }
        else{
            $this->load->view('static_nav_lout');
        }
        $this->load->view('category_content',$data);
        $this->load->view('sign_in');
        $this->load->view('footer'); 

    }

    public function category_notes($sort_by, $order_by)
    {
        if($sort_by=='0')
        $sort_by = 'notes_id';

        if($order_by=='0')
        $order_by = 'ASC';

        $this->load->library('pagination'); 
        $this->load->model('upload_notes');

        //configuring pagination
        $config = $this->style_pagination();
        $config['base_url'] = base_url()."Site/category_notes/$sort_by/$order_by";
        $config['per_page'] = 12;
        $config['num_links'] = 4;
        $config['uri_segment'] = 5;


        //getting data from db
        $data = $this->upload_notes->get_records($config['per_page'], $this->uri->segment(5), $sort_by, $order_by);

        $config['total_rows'] = $data['num_rows'];

        //init pagination
        $this->pagination->initialize($config);

        //cat to view
//        $data['cat'] = $cat;
//        $data['Heading'] = $this->adModel->get_category_name($cat);

        //loading views        
        $this->load->view('header');
        if($this->is_logged_in()){
            $this->load->view('static_nav');
        }
        else{
            $this->load->view('static_nav_lout');
        }
        $this->load->view('notes_posted',$data);
//        $this->load->view('sign_in');
        $this->load->view('footer'); 

    }
    
    public function is_logged_in(){
        //login validation
        $flag = $this->session->userdata('logged_in');
        return $flag;
    }

    public function style_pagination(){
        $config['full_tag_open'] = '<nav> <ul class="pagination">';
        $config['full_tag_close'] = '</ul> </nav>';

        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';

        $config['prev_link'] = '&laquo;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';

        $config['next_link'] = '&raquo;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';

        return $config;
    }

    public function search_ads($sort_by, $order_by, $search)
    {
        if($sort_by=='0')
        $sort_by = 'ad_id';

        if($order_by=='0')
        $order_by = 'ASC';

        $this->load->library('pagination'); 
        $this->load->model('adModel');

        //configuring pagination
        $config = $this->style_pagination();
        $config['base_url'] = base_url()."Site/search_ads/$sort_by/$order_by/$search/";
        $config['per_page'] = 12;
        $config['num_links'] = 5;
        $config['uri_segment'] = 6;

        //getting data from db
        $data = $this->adModel->search($config['per_page'], $this->uri->segment(6), $sort_by, $order_by, $search);

        $config['total_rows'] = $data['num_rows'];

        //init pagination
        $this->pagination->initialize($config);

        //cat to view
        $data['cat'] = -1;
        $data['Heading'] ='Search Results';


        //loading views        
        $this->load->view('header');
        if($this->is_logged_in()){
            $this->load->view('static_nav');
        }
        else{
            $this->load->view('static_nav_lout');
        }
        $this->load->view('category_content',$data);
        $this->load->view('sign_in');
        $this->load->view('footer'); 
    }

    public function search_notes($sort_by, $order_by,$search){
        #hannay write code here and update/use search function in ajency.js 
        if($sort_by=='0')
        $sort_by = 'notes_id';

        if($order_by=='0')
        $order_by = 'ASC';

        $this->load->library('pagination'); 
        $this->load->model('upload_notes');

        //configuring pagination
        $config = $this->style_pagination();
        $config['base_url'] = base_url()."Site/search/$sort_by/$order_by/$search/";
        $config['per_page'] = 12;
        $config['num_links'] = 5;
        $config['uri_segment'] = 6;

        //getting data from db
        $data = $this->upload_notes->search($config['per_page'], $this->uri->segment(6),$sort_by, $order_by, $search);

        $config['total_rows'] = $data['num_rows'];

        //init pagination
        $this->pagination->initialize($config);

        //cat to view
        $data['cat'] = -1;
        $data['Heading'] ='Search Results';


        //loading views        
        $this->load->view('header');
        if($this->is_logged_in()){
            $this->load->view('static_nav');
        }
        else{
            $this->load->view('static_nav_lout');
        }
        $this->load->view('notes_posted',$data);
        $this->load->view('sign_in');
        $this->load->view('footer'); 
    }

    public function search_users($sort_by, $order_by, $search)
    {
        if($sort_by=='0')
        $sort_by = 'user_id';

        if($order_by=='0')
        $order_by = 'ASC';

        $this->load->library('pagination'); 
        $this->load->model('userModel');

        //configuring pagination
        $config = $this->style_pagination();
        $config['base_url'] = base_url()."Site/search_users/$sort_by/$order_by/$search/";
        $config['per_page'] = 12;
        $config['num_links'] = 5;
        $config['uri_segment'] = 6;

        //getting data from db
        $data = $this->userModel->searchUsers($config['per_page'], $this->uri->segment(6), $sort_by, $order_by, $search);

        $config['total_rows'] = $data['num_rows'];

        //init pagination
        $this->pagination->initialize($config);

        //cat to view
        $data['search_q'] = $search;
        $data['Heading'] ='Search Results';


        //loading views        
        $this->load->view('header');
        if($this->is_logged_in()){
            $this->load->view('static_nav');
        }
        else{
            $this->load->view('static_nav_lout');
        }
        $this->load->view('user_results_content',$data);
        $this->load->view('sign_in');
        $this->load->view('footer'); 
    }
    
    public function view_profile($id)
    { 
        $this->load->model('userModel');

        $data['userData'] = $this->userModel->getUserData($id)[0];
        $data['uProfile'] = true;
        $data['id'] = $id;

        $this->load->view('header');
        if($this->is_logged_in()){
            $this->load->view('static_nav');
        }
        else{
            $this->load->view('static_nav_lout');
        }
        $this->load->view('user_profile_content',$data);
        $this->load->view('footer'); 

    }

    public function profile_content_ads($id){

        $this->load->model('userModel');
        $data['adData'] = $this->userModel->getUserAds($id);
        $this->load->view('user_profile_ads',$data);

    }

    public function profile_content_notes($id){

        $this->load->model('userModel');
        $data['records'] = $this->userModel->getUserNotes($id);
        $this->load->view('user_profile_Notes',$data);

    }

    

    
}


/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */